/**
 * @description:
 * @param {*}
 * @return {*}
 */
import axios from 'axios'
import { getToken } from '@/utils/auth'

import store from '@/store'
import { containsAny, containsAll } from '@/utils/array-util'

export function goBacks() {
  this.$router.go(-1)
}
export function Verify(content) {
  if (!content.replace(/(^\s*)|(\s*$)/g, '') === '') {
    return false
  }
  return true
}
export function uploadDocument(formData) {
  axios({
    url: process.env.VUE_APP_BASE_API + '/api/fd/document/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data;charset=utf-8',
      Authorization: getToken()
    }
  })
    .then((res) => {
      if (res.data.code === 1) {
        this.$message.success('上传成功')
        this.fileList = []
        this.$refs.upload.clearFiles()
        this.$parent.getList()
        this.$emit('uploadRes', res)
      }
      this.$message.error('上传失败')
    })
    .catch((err) => {
      console.log(err)
    })
}

export function arrDifferent(data) {
  for (var i = 0; i < data.length; i++) {
    for (var j = i + 1; j < data.length; j++) {
      if (data[i].id === data[j].id) {
        data.splice(j, 1)
        j--
      }
    }
  }
  return data
}

import Sortable from 'sortablejs'

// 列拖拽
export function columnDrop(sortable, dropCol) {
  const wrapperTr = document.querySelector('.el-table__header-wrapper tr')
  sortable = Sortable.create(wrapperTr, {
    animation: 180,
    delay: 0,
    onEnd: (evt) => {
      const oldItem = dropCol[evt.oldIndex]
      dropCol.splice(evt.oldIndex, 1)
      dropCol.splice(evt.newIndex, 0, oldItem)
    }
  })
}

/**
 * 判断当前用户是否具有任一个角色
 *
 * @param {*} e 角色
 * @returns 返回true表示具有，否则返回false表示不具有
 */
export function isInRole(e) {
  return containsAny(store.state.user.roles, e)
}

/**
 * 判断当前用户是否具有全部的角色
 *
 * @param {*} e 角色
 * @returns 返回true表示具有，否则返回false表示不具有
 */
export function isInAllRole(e) {
  return containsAll(store.state.user.roles, e)
}

// 树结构展开
export function expandArr(arr) {
  let result = []
  arr.forEach((item) => {
    var res = JSON.parse(JSON.stringify(item))
    delete res['children']
    result.push(res)
    if (item.children instanceof Array && item.children.length > 0) {
      result = result.concat(expandArr(item.children))
    }
  })
  return result
}
export function expandArrUser(arr) {
  let result = []
  arr.forEach((item) => {
    if (item.userResponses instanceof Array && item.userResponses.length > 0) {
      item.userResponses.forEach(e => {
        e.groupName = item.label
        result.push(e)
      })
    }
  })
  return result
}

/**
 * 两数组合并去重
 * @param {Array} aData 数组1
 * @param {Array} bData 数组2
 * @param {string} isTerm 判断相同条件
 * @returns {Array} newArr
 */
export function clearRepetitive(aData, bData, isTerm) {
  let newArr = aData || []
  const list = bData
  newArr.forEach((e) => {
    bData.forEach((res, index) => {
      if (e[isTerm].indexOf(res[isTerm]) !== -1) {
        list.splice(index, 1)
      }
    })
  })
  newArr = [...newArr, ...list]
  return newArr
}

/**
 * 两数组合并去重,返回不同数组
 * @param {Array} aData 数组1
 * @param {Array} bData 数组2
 * @param {string} isTerm 判断相同条件
 * @returns {Array} newArr
 */
export function clearRepetitiveNewArr(aData, bData, isTerm) {
  const newArr = aData.map(item => item.id)
  const result = bData.filter(item => !newArr.includes(item.id))
  const res = { arr: bData, res: result }

  return res
}

/**
 * 处理提交数据
 * @param {Array} Arr 待处理数组
 * @param {string} name 提交的参数属性name
 * @returns {Array} newArr
 */
export function clearPushArr(Arr, name) {
  const newArr = []
  Arr.forEach(e => {
    newArr.push(e[name])
  })
  return newArr
}

/**
 * 判断罗个数组是否相同
 * @param {Array} a1 数组1
 * @param {Array} a2 数组2
 * @returns {string} is
 */
export function isArrEqual(a1, a2) {
  if (a1 === a2) return true
  if ((!a1 && a2) || (a1 && !a2)) return false
  if (a1.length !== a2.length) return false
  for (var i = 0, n = a1.length; i < n; i++) {
    if (a1[i].content !== a2[i].content) return false
  }
  return true
}

/**
 * 数组重排序
 * @param {Array} Arr 数组
 * @param {string} code 根据排序的属性
 * @returns {Array} Arr
 */
export function newSort(Arr, code) {
  // console.log(Arr,code,'orororororo');
  const newArr = Arr || []
  newArr.sort(function (a, b) {
    if (a[code] != b[code]) {
      return a[code].localeCompare(b[code])
    }
  })
  return newArr
}

/**
 * 计算项目实际
 * @param {Array} Arr 数组
 * @param {string} code 根据排序的属性
 * @param {string} type 1为需要对数组重新排序，2不需要
 * @returns {Array} Arr
 */
export function calculateFactQuantity(Arr, index, _this, code) {
  // console.log(Arr,code,'orororororo');
  const newArr = Arr || []
  let factQuantity = 0
  let agoIndex = 0
  newArr.forEach((e, index2) => {
    if (e.type === 1 && e.goodsCode === code) {
      factQuantity = factQuantity + Number(e.factQuantity)
    } else {
      agoIndex = index2
    }
  })
  // if (newArr[agoIndex].factQuantity < factQuantity) {
  //   _this.$message.warning('输入的实际数量和大于计划')
  //   newArr[agoIndex].factQuantity = newArr[agoIndex].expectQuantity
  //   newArr.forEach(e => {
  //     if (e.type === 1) {
  //       e.factQuantity = e.expectQuantity
  //     }
  //   })
  // }
  return newArr
}
/**
 * 简单实现防抖方法
 * @param fn 要防抖的函数
 * @param delay 防抖的毫秒数
 * @returns {Function}
 */
export function debounce(fn, delay) {
  let timer = null
  return function () {
    const args = arguments
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      fn.apply(this, args)
    }, delay)
  }
}

/**
 * @description: 节流
 * @param {*}
 * @return {*}
 */
export function thorttle(fn, delay = 1000) {
  let lastTime = ''
  let timer = ''
  const interval = delay
  return function () {
    const args = arguments
    const nowTime = Date.now()
    if (lastTime && nowTime - lastTime < interval) {
      clearTimeout(timer)
      timer = setTimeout(() => {
        lastTime = nowTime
        fn.apply(this, args)
      }, interval)
    } else {
      lastTime = nowTime
      fn.apply(this, args)
    }
  }
}
